<template>
  <el-scrollbar>
    <div>
      <div style="margin-bottom: 5px; margin-top: 5px;">
        <el-input v-model="title" 
        placeholder="请输入文章标题" :suffix-icon="SearchIcon" style="width: 300px; margin-left: 4px;"
        @keyup.enter.native="getData"></el-input>
      <el-button type="primary" style="margin-left: 10px;" @click="getData">查询</el-button>
      <el-button type="danger" @click="resetParam">重置</el-button>
      
    </div>
          <el-table :data="tableData" 
          :header-cell-style="{background: '#B0C4DE', color:'#555','text-align': 'center'}"
                :cell-style="{padding:17+'px','text-align': 'center'}" 
                :border-color="black"
                style="margin-top:15px; font-size: 15px">
            <el-table-column prop="id" label="ID" width="80" />
            <el-table-column prop="title" label="标题" width="410" />
            <el-table-column prop="updatetime" label="发布时间" width="300"  />         
            <el-table-column prop="operate" label="操作" >
              <template #default="scope">
              
  <el-button type="primary" style="margin-left: 14px" @click="getDetail(scope.row)">
    查看全文>>></el-button>
           </template>
              </el-table-column>            
          </el-table>
          <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[2, 5, 10, 15]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"/>
      <el-dialog v-model="centerDialogVisible" title="提示" width="30%" center>
    <span>
      <el-form :rules="rules" :model="form" ref="form" label-width="120px">
        <el-form-item label="标题" prop="title">
      <el-input v-model="form.title" />
    </el-form-item>
    <el-form-item label="内容" prop="content">
      <el-input v-model="form.content" />
    </el-form-item>
    </el-form>
    </span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
        </div>
        </el-scrollbar>
        <el-drawer
    v-model="drawer2"
    :direction="direction"
    :before-close="handleClose">
  <h3>{{ title }}</h3>
  <br>
    <p>{{ row.content }}</p>
  </el-drawer>
</template>

<script>
import axios from 'axios';
import { Search } from '@element-plus/icons-vue'
export default {
  data() {
    return { 
      tableData:[],
      pageSize: 10,
      pageNum: 1,
      total: 0,
      SearchIcon:Search,
      title:'',
      row:'',
      centerDialogVisible:false,
      drawer2: false,
      direction:'rtl',
      form:{
        title:'',
        content:''
      },
      form1:{
        content:''
      },
      rules: {
        title: [
                    {required: true, message: '请输入标题', trigger: 'blur'}      
        ],
        content: [
                      {required: true, message: '请输入内容', trigger: 'blur'}
                    ]
      }
    }
  },
  created(){
    this.getData();
  },
  methods:{
    cancelClick() {
  this.drawer2 = false
},
    getDetail(row){
      this.row = row;
      this.title = row.title;      
      this.drawer2 = true
    },
    getData() {
      axios.post('/news/listPageR',{
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        param: {
            title:this.title
         
        }
      }).then(response=> { 
        console.log(response.data)
        if(response.data.code == 200){
          this.tableData = response.data.data;
          this.total = response.data.total
        }else{
          alert('获取数据失败')
        }
      }).catch(function (error) {
        console.log(error);
      });
    },
    
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum = 1
      this.pageSize = val
      this.getData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val
      this.getData();
    },
    resetParam(){
      this.title=''
    },
    add(){
      this.centerDialogVisible = true;
      this.$nextTick(()=>{
        this.resetForm();
        this.form.id = ''
      })
    },
    doSave(){
  axios.post('/news/save', JSON.stringify(this.form), {
    headers: {
      'Content-Type': 'application/json'
    }
  }).then(response => { 
    console.log(response.data)
    if(response.data.code == 200){
      this.$message({
        message:'新增成功！',
        type: 'success'
      });
      this.centerDialogVisible = false;
      this.getData();
    }else{
      this.$message({
        message:'新增失败，请重试！',
        type: 'error'
      });
    }
  })
},
    doMod(){
      axios.post('/news/update',this.form).then(response=> { 
        console.log(response.data)
        if(response.data.code == 200){
         this.$message({
              message:'修改成功！',
              type: 'success'
         });
         this.centerDialogVisible = false;
         this.getData();
        }else{
          this.$message({
              message:'修改失败，请重试！',
              type: 'error'
         });
        }
      })
    },
    save(){
      this.$refs.form.validate((valid) => {
                    if (valid) {
                      if(this.form.id) {
                        this.doMod();
                      }else{
                        this.doSave();
                      }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });   
    },
    resetForm(){
      this.$refs.form.resetFields();
    },
    mod(row){
      console.log(row);
      this.centerDialogVisible = true;
      this.form = row;
    },
    del(id){
      //console.log(id);
      axios.get('/news/del?id='+id).then(response=> { 
        console.log(response.data)
        if(response.data.code == 200){
         this.$message({
              message:'删除成功！',
              type: 'success'
         });
         this.getData();
        }else{
          this.$message({
              message:'删除失败，请重试！',
              type: 'error'
         });
        }
      })
    }
  }
}
  
</script>